<?php

namespace app\admin\controller;

use think\Image;
use think\Controller;
use think\Request;
use think\Db;
use think\Validate;
//分页功能
use think\Paginator;
use think\Paginator\driver\Bootstrap;

class Teasort extends AdminController
{
    //展示页面
    public function index()
    {
        $a = DB::name('teasort')
            ->select();
        return view('', ['data' => $a]);
    }

    public function add()
    {
        return view();
    }

    public function save(Request $request)
    {
        $p = $request->post();
        $a = DB::name('teasort')
            ->field('teasort')
            ->select();
        $b = [];
        foreach ($a as $k => $v) {
            $b[] = $v['teasort'];
        }
        //验证
       $rule = [
          'teasort' => 'require|max:25|chsDash',
      ];

       $msg = [
      'teasort.require' => '教师类型不能为空',
      'teasort.max' => '教师类型名称过长',
      'teasort.chsDash'=>'文章类型只能是汉字、字母、数字和下划线_及破折号-'
      ];

      $validate = new Validate($rule,$msg);
      if(!$validate->check($data)){
      $this->error($validate->getError());
      }
        if (in_array($p['teasort'], $b)) {
            $this->error('分类已存在,请重新添加');
        } else {
            $res = DB::name('teasort')
                ->data($p)
                ->insert();
            if ($res > 0) {
                return $this->success('添加成功', url('admin/teasort/index'));
            } else {

                return $this->error('添加失败,请重试');
            }

        }
    }

    //编辑分类(edit)
    public function edit($id)
    {
        //查数据
        $data = Db::name('teasort')
            ->where('id', $id)
            ->find();

        return view('', ['data' => $data]);
    }

    //更新数据
    public function update(Request $request, $id)
    {
        $p = $request->post();
        // var_dump($p);die;
        $a = DB::name('teasort')
            ->field('teasort')
            ->select();
        $b = [];
        foreach ($a as $k => $v) {
            $b[] = $v['teasort'];
        }

         //验证
       $rule = [
          'teasort' => 'require|max:25',
      ];

       $msg = [
      'teasort.require' => '教师类型不能为空',
      'teasort.max' => '教师类型名称过长',
      ];

      $validate = new Validate($rule,$msg);
      if(!$validate->check($data)){
      $this->error($validate->getError());
      }
        // var_dump($b);die;
        if(in_array($p['teasort'],$b)){
         $this->error('分类已存在,请重新编辑');
        }else{
        $res=DB::name('teasort')
              ->where('id',$id)
              ->update($p);

         if($res > 0){
                    return $this ->success('编辑成功',url('admin/teasort/index'));
                }else{

                    return $this->error('编辑失败,请重试');
                }

            }
      }

    //删除分类
    public function delete()
    {
        // return '123';
        $id = input('id');
        // dump($id);
        $a = Db::name('teacher')
            ->where('teasid', $id)
            ->select();
        // var_dump($a);die;
        if (!empty($a)) {
            $this->error('该分类下还有教师,不能删除');}
      else{
           $res = Db::name('teasort')
                    ->delete($id);
           if($res > 0){
                    return $this ->success('删除成功',url('admin/teasort/index'));
                }else{

                    return $this->error('删除失败,请重试');
                }
                  }
         }
}








